#!/bin/bash
# 获取脚本路径
current_dir=$(cd $(dirname $0); pwd)
# 导入配置文件
source ${current_dir}/config.cnf

function check_etcd(){
  echo "------------- 查看ETCD的list --------------"
  etcdctl member list
  echo ""
  echo "------------- 查看ETCD的健康状态 --------------"
  etcdctl cluster-health
}

function check_patroni(){
  echo "查看patroni的list"
  patronictl -c /etc/patroni/patroni.yml list 
}

# 检查etcd中存储的postgres主节点是哪个
function check_etcd_postgres_primary(){
  export ETCDCTL_API=3
  #echo "etcdctl --endpoints=${local_ip}:${ectd_client_port} get ${pg_dir}/patroni/pgsql/leader|grep -v ${pg_dir}/patroni/pgsql/leader"
  etcdctl --endpoints=${local_ip}:${ectd_client_port} get ${pg_dir}/patroni/pgsql/leader|grep -v ${pg_dir}/patroni/pgsql/leader
}

function main(){
  if [ "$1" == "es" ];then
    check_etcd
  elif [ "$1" == "ps" ];then
    check_patroni
  elif [ "$1" == "check_primary" ];then
    check_etcd_postgres_primary
  else
    echo "
      $0 es   			检查 ETCD 的状态
      $0 ps   			检查 patroni 的状态
      $0 check_primary 		查看etcd中postgres当前主
    "
  fi
}

main $1
